/* ASSIGNMENT 1
* File: Flight.java
* Date: 08/28/2012
*/


import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * A Flight represents one connection between airports (it's an edge
 * in a graph of airport nodes). The Flight can have up to three FlightSections,
 * but it doesn't necessarily have any. The Flight cannot have any seats without having
 * FlightSections. A Flight also must belong to an airline. 
 */

public class Traveling {

	//Define the maximum and minimum number of ID characters,
	public final static int MAXIDCHARS = 6;
	public final static int MINIDCHARS = 3;
	
	/*//All flights must be scheduled after this day
	public final static int AFTERYEAR = 2013;
	public final static int AFTERMONTH = 12;
	public final static int AFTERDAY = 31;
	public final static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy");
	*/
	protected String id;
	//protected TravelCompany owner;
	
	
	/*Constructs new Flight objects.
	*There are restrictions on flight ids, which are enforced in the constructor.
	*The max and min characters are defined statically, and requirements are
	*handled through a separate checking method, for easy modularity.
	*/
	//public Traveling(TravelCompany ownerArg, ParentPort originArg, ParentPort destinationArg, Calendar dateArg, String idArg) throws ManagementException
	public Traveling(String idArg) throws ManagementException
	
	{
		//Check to see if the string is null
		if(idArg == null)
		{
			throw new ManagementException("You are attempting to create a flight without a null ID. All flights must have IDs.");
		}
		idArg = idArg.toUpperCase();
		boolean nameIsOK = checkId(idArg);
		
		if(!nameIsOK)
		{
			throw new ManagementException("Flight ID must have between " + MINIDCHARS + " and " + MAXIDCHARS + " alphanumeric characters, the length is " 
					+ idArg.length() + " for ID " + idArg);
		}

		//Assign id and owner so that calls to this.toString() can resolve.
		id = idArg;
		
	}
	
	//Performs a check on the ID.
	private boolean checkId(String idArg) throws ManagementException
	{
		boolean nameIsOK = true;
		
		//Use a regex to ensure that only alphanumeric characters are in the ID
		Pattern p = Pattern.compile(".*\\W+.*");
		Matcher m = p.matcher(idArg);
		boolean containsNonAlphanumeric = m.matches();
		
		if(idArg.length() < MINIDCHARS || idArg.length() > MAXIDCHARS)
		{
			nameIsOK = false;
		}
		if(containsNonAlphanumeric)
		{
			nameIsOK = false;		
		}
		
		return nameIsOK;
	}
	

	
	public String toString()
	{
		return id;
	}
	
	/*==========
	   Getters
	  ==========*/
	public String getId()
	{
		return id;
	}

}
